package ue.core;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import ue.galaxy.Location;
import ue.networking.Server;

public class Galaxy {
	
	public static ArrayList<Location> locations;
	public static Server server;
	
	public static void main(String[] args) {
		locations = new ArrayList<Location>();
//		Location location = new Location(2356, 324);
//		location.saveToXMLFile("location.xml");
		
		System.out.println(Galaxy.getTimestamp() + "Server started");
		
		server = new Server(7777);
		for(;;) {
			
			server.run();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
//		AccountSystem system = new AccountSystem();
//		
//		system.addAccount("asdf", "A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ=");
//		
//		system.saveToXML("res/accounts", "accountSystem.xml");
	}
	
	public static int findLocation(int x, int y) {
		for (int i = 0; i < locations.size(); i++) {
			if (locations.get(i).getX() == x && locations.get(i).getY() == y) {
				return i;
			}
		}
		
		return -1;
	}
	
	public static Location getLocation(int x, int y) {
		int i = findLocation(x, y);
		
		if(i < 0) {
			locations.add(new Location(x, y));
			i = locations.size() - 1;
		}
		
		return locations.get(i);
	}
	
	
	public static String getTimestamp() {
		return "[" + new SimpleDateFormat("yyyy.MM.dd:HH.mm.ss.SSS").format(new Date()) + "]";
	}
}
